<template>
    <div class="content">
        <mt-header fixed title="请选择品类">
            <div slot="left">
                <mt-button icon="back" @click.native="goback()">返回</mt-button>
            </div>
        </mt-header>
       <mt-loadmore  :bottom-method="loadBottom" :bottom-all-loaded="allLoaded"   :autoFill="autoFill"  ref="loadmore">
            <div class="img" v-for="item of devicecategorys" @click.native="selectItem(item.devicecategoryid)" @touchstart="selectItem(item.devicecategoryname)" :key="item.devicecategoryid" >
                <img :src="item.devicecategoryurl">
                <span>{{item.devicecategoryname}}</span>
            </div>
         </mt-loadmore>
    </div>
</template>
<script>
import wx from 'weixin-js-sdk'
import notDataPage from '@/component/notQueryDataPage.vue'
export default {
  data () {
    return {
      devicecategorys: [],
      param: {
        page: 1,
        pagesize: 50
      },
      allLoaded: false,
      autoFill: false,
      pages: 0

    }
  },
  components: {
    notDataPage
  },
  created () {
    this.getCategory()
  },
  methods: {
    goback () {
      this.$router.go(-1)
    },
    async selectItem (devicecategoryid) {
      const that = this
      const parm = {
        type: 1,
        channelid: 1,
        localurl: window.location.href
      }
      const res = await this.$store.dispatch('getWeiXinParam', parm)
      if (res.status === '0') {
        // 这里【url参数一定是去参的本网址】
      // this.axios.get('获取微信认证参数的地址?url=去参的本网页地址', function (data) {
      // var jsondata = JSON.parse(data)
        wx.config({
        // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
          debug: true,
          // 必填，公众号的唯一标识
          appId: res.context.appid,
          // 必填，生成签名的时间戳
          timestamp: res.context.timestamp,
          // 必填，生成签名的随机串
          nonceStr: res.context.noncestr,
          // 必填，签名
          signature: res.context.signature,
          // 必填，需要使用的JS接口列表，所有JS接口列表
          jsApiList: ['checkJsApi', 'scanQRCode']
        })
        // })
        wx.error(function (res) {
          // alert('出错了：' + res.errMsg) // 这个地方的好处就是wx.config配置错误，会弹出窗口哪里错误，然后根据微信文档查询即可。
        })
        wx.ready(function () {
          wx.checkJsApi({
            jsApiList: ['scanQRCode'],
            success: function (res) {}
          })

          wx.scanQRCode({
            needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
            // scanType: ['qrCode'], // 可以指定扫二维码还是一维码，默认二者都有
            success: function (res) {
              var result = res.resultStr // 当needResult 为 1 时，扫码返回的结果
              alert('扫描结果：' + result)
              that.$router.push({
                path: '/addEquipment',
                query: {
                  deviceid: res.resultStr,
                  devicecategoryid
                }
              })
            }
          })
        })
      }
    },
    async getCategory (type) {
      const res = await this.$store.dispatch('accetcategory', this.param)
      if (res.status === '0') {
        this.pages = res.context ? res.context.length : 0
        type ? this.devicecategorys = [...this.devicecategorys, ...res.context] : this.devicecategorys = [...res.context]
      }
    },
    async loadBottom () {
      this.$refs.loadmore.onBottomLoaded()
      if (this.pages < this.param.pagesize) {
        this.allLoaded = true
        return
      }
      this.param.page++
      await this.getAlarmList(1)
    }
  }
}
</script>
<style lang="less" scoped>
.content {
    position: absolute;
    top: 44px;
    left: 0;
    right: 0;
    bottom: 0;
    &>div{
      width: 353px;
      margin: 0 auto;
    .img {
		display: inline-block;
		width: 80px;
		text-align: center;
    margin: 0 4px;
		img {
			width: 100%;
		}
		span {
            display: inline-block;
            width: 100%;
            height: 30px;
            line-height: 30px;
			word-break: keep-all;
			overflow: hidden;
			white-space: nowrap;
			text-overflow: ellipsis;
		}
	}

    }

}
</style>
